#include <avr/io.h>     
#include <util/delay.h>

// Function prototypes
int     wait_one_second(void);
int     set_PORTB_bit(int position, int value);

// This program will merely blink the LEDs on PB3 and PB4, but the code
// illustrates using the new functions, including the one that uses
// a bunch of binary operators to set or clear specific bits in PORTB 
// while leaving the other bits unchanged.
int main(void)
{
  // Set Port B pins for 3 and 4 as outputs
  // PORTB bit 3 = physical pin #2 on the ATTINY45
  // PORTB bit 4 = physical pin #3 on the ATTINY45
  DDRB = 0x18;  // In binary this is 0001 1000 (note that is bit 3 and 4)

  // Set up a forever loop using 'C'-style for loop
  // i.e. loop while '1' equals '1'
  for ( ; 1==1 ; )
  {
    set_PORTB_bit(3, 1);    // Set bit 3 high
    set_PORTB_bit(4, 1);    // Set bit 4 high
    set_PORTB_bit(5, 1);    // Set bit 5 high
    wait_one_second();
    set_PORTB_bit(3, 0);    // Set bit 3 low, leaving bit 4 unchanged
    set_PORTB_bit(5, 0);    // Set bit 5 low, leaving bit 4 unchanged
    wait_one_second();
  }

  return 1;
}

// Functions
int wait_one_second(void)
{
        _delay_ms(1000);
        return 1;
}

int set_PORTB_bit(int position, int value)
{
        // Sets or clears the bit in position 'position' 
        // either high or low (1 or 0) to match 'value'.
        // Leaves all other bits in PORTB unchanged.
        
        if (value == 0)
        {
                PORTB &= ~(1 << position);      // Set bit # 'position' low
        }
        else
        {
                PORTB |= (1 << position);       // Set bit # 'position' high
        }
        return 1;
}
